package com.csh.szhm.adapter;

import java.util.ArrayList;
import java.util.List;

import com.csh.szhm.R;
import com.csh.szhm.SzhmApplication;
import com.csh.szhm.activity.SzhmSortThreeMainActiviy;
import com.csh.szhm.activity.SzhmSortTwoMainActiviy;
import com.csh.szhm.custorm.autoviewpager.AutoScrollViewPager;
import com.csh.szhm.custorm.autoviewpager.CirclePageIndicator;
import com.csh.szhm.custorm.autoviewpager.ImagePagerAdapter;
import com.csh.szhm.http.HttpUrls;
import com.csh.szhm.module.recommend.MainRecommenListItemBean;
import com.csh.szhm.utils.DebugUntil;
import com.csh.szhm.utils.StringUtil;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SzhmMainAdapter extends MyBaseAdapter<MainRecommenListItemBean>
		implements OnClickListener {

	private static final int TYPE_ONE = 0;
	private static final int TYPE_TWO = 1;
	private static final int TYPE_THREE = 2;
	private int[] itemImg = new int[] { R.drawable.main_center_1,
			R.drawable.main_center_2, R.drawable.main_center_3,
			R.drawable.main_center_4, R.drawable.main_center_5,
			R.drawable.main_center_7 };
	private String[] itemText = new String[] { "餐饮美食", "宾馆酒店", "生活服务", "时尚购物",
			"休闲娱乐", "鼎丰建材" };
	private int[] includeView = new int[] { R.id.main_center_1,
			R.id.main_center_2, R.id.main_center_3, R.id.main_center_4,
			R.id.main_center_5, R.id.main_center_6 };

	public SzhmMainAdapter(Context c) {
		super(c);
	}

	@Override
	public int getCount() {
		return mData.size() + 2;
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return TYPE_ONE;
		} else if (position == 1) {
			return TYPE_TWO;
		} else {
			return TYPE_THREE;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HolderView holderView = null;
		HolderViewTop holderViewTop = null;
		HolderViewCenter holderViewCenter = null;
		int type = getItemViewType(position);

		switch (type) {
		case TYPE_ONE:
			if (convertView == null) {
				holderViewTop = new HolderViewTop();
				convertView = mLayoutInflater.inflate(
						R.layout.main_one_top_layout, null);
				holderViewTop.vp = (AutoScrollViewPager) convertView
						.findViewById(R.id.main_view_pager);
				holderViewTop.ci = (CirclePageIndicator) convertView
						.findViewById(R.id.main_indicator);
				convertView.setTag(holderViewTop);
			} else {
				holderViewTop = (HolderViewTop) convertView.getTag();
			}
			initViewView(convertView);
			break;
		case TYPE_TWO:
			if (convertView == null) {
				holderViewCenter = new HolderViewCenter();
				convertView = mLayoutInflater.inflate(
						R.layout.main_one_center_layout, null);
				holderViewCenter.center1 = (LinearLayout) convertView
						.findViewById(R.id.main_center_1);
				holderViewCenter.center2 = (LinearLayout) convertView
						.findViewById(R.id.main_center_2);
				holderViewCenter.center3 = (LinearLayout) convertView
						.findViewById(R.id.main_center_3);
				holderViewCenter.center4 = (LinearLayout) convertView
						.findViewById(R.id.main_center_4);
				holderViewCenter.center5 = (LinearLayout) convertView
						.findViewById(R.id.main_center_5);
				holderViewCenter.center6 = (LinearLayout) convertView
						.findViewById(R.id.main_center_6);
				convertView.setTag(holderViewCenter);
			} else {
				holderViewCenter = (HolderViewCenter) convertView.getTag();
			}
			initCenterView(convertView);
			break;
		case TYPE_THREE:
			if (convertView == null) {
				holderView = new HolderView();
				convertView = mLayoutInflater.inflate(
						R.layout.main_item_list_layout, null);
				holderView.img = (ImageView) convertView
						.findViewById(R.id.main_list_item_img_id);
				holderView.title = (TextView) convertView
						.findViewById(R.id.main_list_item_title_id);
				holderView.statu = (TextView) convertView
						.findViewById(R.id.main_list_item_statu_id);
				holderView.tj = (TextView) convertView
						.findViewById(R.id.main_item_tj_id);
				convertView.setTag(holderView);
			} else {
				holderView = (HolderView) convertView.getTag();
			}
			holderView.title.setText(mData.get(position - 2).getFirmTitle());
			String dis = mData.get(position - 2).getDiscount();
			if (StringUtil.isEmptyString(dis)) {
				holderView.statu.setText("会员价");
			} else {
				holderView.statu.setText("全单" + dis + "折");
			}
			SzhmApplication.display(HttpUrls.PATH_ROOT
					+ mData.get(position - 2).getFirmImagePath(),
					holderView.img);
			break;
		}

		return convertView;
	}

	private class HolderView {
		ImageView img;
		TextView title;
		TextView statu;
		TextView tj;
	}

	private class HolderViewTop {
		AutoScrollViewPager vp;
		CirclePageIndicator ci;
	}

	private class HolderViewCenter {
		LinearLayout center1;
		LinearLayout center2;
		LinearLayout center3;
		LinearLayout center4;
		LinearLayout center5;
		LinearLayout center6;
	}

	private void initCenterView(View v) {
		for (int i = 0; i < itemImg.length; i++) {
			ImageView img = (ImageView) v.findViewById(includeView[i])
					.findViewById(R.id.main_center_img_id);
			TextView txt = (TextView) v.findViewById(includeView[i])
					.findViewById(R.id.main_center_txt_id);
			v.findViewById(includeView[i])
					.findViewById(R.id.main_center_root_id)
					.setOnClickListener(this);
			v.findViewById(includeView[i])
					.findViewById(R.id.main_center_root_id).setTag(i);
			img.setBackgroundResource(itemImg[i]);
			txt.setText(itemText[i]);
		}
	}

	private AutoScrollViewPager mViewPager;
	private CirclePageIndicator mCirclePage;
	private ImagePagerAdapter<String> mImagePagerAdapter;

	private void initViewView(View mView) {
		if (mViewPager == null) {
			mViewPager = (AutoScrollViewPager) mView
					.findViewById(R.id.main_view_pager);
			mCirclePage = (CirclePageIndicator) mView
					.findViewById(R.id.main_indicator);
			mImagePagerAdapter = new ImagePagerAdapter<String>(mContext);
			mImagePagerAdapter.setInfiniteLoop(true);
			mViewPager.setAdapter(mImagePagerAdapter);
			mViewPager.setDirection(AutoScrollViewPager.RIGHT);
			mViewPager.setScrollDurationFactor(2);
			mViewPager.setStopScrollWhenTouch(true);
			mViewPager.setBorderAnimation(true);
			mViewPager
					.setSlideBorderMode(AutoScrollViewPager.SLIDE_BORDER_MODE_TO_PARENT);
			mViewPager.setCycle(false);
			mViewPager.setInterval(5000);
			mViewPager.startAutoScroll();
			mCirclePage.setViewPager(mViewPager);
			mViewPager.setCurrentItem(5);
			mCirclePage.setOnPageChangeListener(new OnPageChangeListener() {

				@Override
				public void onPageSelected(int arg0) {
					int i = ((arg0) % mCirclePage.getCount());
					mCirclePage.setCurrentItem(i);
				}

				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {

				}

				@Override
				public void onPageScrollStateChanged(int arg0) {

				}
			});
		}
		List<String> data = new ArrayList<String>();
		data.add("upload/attachment/20140707/77.jpg");
		mImagePagerAdapter.clear();
		mImagePagerAdapter.addData(data);
		mCirclePage.setCount(data.size());
	}

	@Override
	public void onClick(View v) {
		Intent in = new Intent();
		if (v.getId() == R.id.main_center_root_id) {
			int tag = (Integer) v.getTag();
			switch (tag) {
			case 0:
				in.putExtra("type", 1);
				startMyActivity(in, SzhmSortThreeMainActiviy.class);
				break;
			case 1:
				in.putExtra("type", 2);
				startMyActivity(in, SzhmSortThreeMainActiviy.class);
				break;
			case 2:
				in.putExtra("type", 3);
				startMyActivity(in, SzhmSortThreeMainActiviy.class);
				break;
			case 3:
				in.putExtra("type", 4);
				startMyActivity(in, SzhmSortThreeMainActiviy.class);
				break;
			case 4:
				in.putExtra("type", 5);
				startMyActivity(in, SzhmSortThreeMainActiviy.class);
				break;
			case 5:
//				DebugUntil.createInstance().toastStr("敬请期待！");
				in.putExtra("type", 152);
				startMyActivity(in, SzhmSortThreeMainActiviy.class);
				// in.putExtra("type", 5);
				// startMyActivity(in, SzhmSortTwoMainActiviy.class);
				break;
			}
		}

	}

}
